define(['jquery', 'bootstrap', 'backend', 'table', 'form','jstree'], function ($, undefined, Backend, Table, Form,) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'old/one/index' + location.search,
                    add_url: 'old/one/add',
                    // edit_url: 'old/one/edit',
                    del_url: 'old/one/del',
                    multi_url: 'old/one/multi',
                    import_url: 'old/one/import',
                    table: 'list_one',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {
                            field: 'status',
                            title: __('状态'),
                            operate: false,
                            formatter: function(value, row, index) {
                                // console.log('index',row)
                                var html = '';
                                if(!row.tong_number){
                                    html = "<p style=\"color:#32f800;font-weight: 700;border: 1px solid #32f800;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                        "                        暂存</p>";
                                }else{
                                    if(row.status == 6){
                                        html = "<p style=\"color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        退统</p>";
                                    }else if(row.status == 7){
                                        html = "<p style=\"color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        作废</p>";
                                    }else{
                                        html = " <p style=\"color:#000;font-weight: 700;border: 1px solid #000;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        正常</p>";
                                    }
                                }
                                return html;
                            }
                        },
                        {
                            field: 'gs_type',
                            title: __('所属公司'),
                            searchList: {
                                1: __('茗延安全服务'),
                                2: __('国寿汽车服务'),
                                3: __('华安行汽车服务'),
                                4: __('新的公司(勿动)'),
                                5: __('海南国寿联动汽车服务'),
                                6: __('安行在线'),
                                7: __('海南大地联合汽车服务'),
                            },
                            custom: {
                                0: 'success',
                                1: 'default',
                                2: 'warning',
                                3: 'info',
                                4: 'danger',
                                5: 'primary',
                                6: 'huang',
                                7: 'zi'
                            },
                            formatter: Table.api.formatter.label
                        },
                        {field: 'tong_storage', title: __('暂存单号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function(value, row, index) {
                                if(value){
                                    return value;
                                }else{
                                    return '-';
                                }
                            }},
                        {field: 'tong_number', title: __('服务单号'), operate: 'LIKE', table: table, class: 'autocontent',formatter: function(value, row, index) {
                                if(value){
                                    return value;
                                }else{
                                    return '-';
                                }
                            }
                        },
                        {field: 'zuzhi.name', title: __('组织'), searchList:$.getJSON('lists/one/select_zuzhi'), table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'car_num', title: __('车牌号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'car_vin', title: __('车架号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'car_engine', title: __('发动机号'),visible:false, operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'learned_name', title: __('被服务人'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'yw_join_time', title: __('服务日期'), operate: false, addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'yw_hetong_time', title: __('审核日期'), operate: false, addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'plan_strattime', title: __('服务起期'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'plan_endtime', title: __('服务止期'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'chexing', title: __('车型'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'ncd', title: __('NCD'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'main_sum', title: __('标准服务费'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'main_sumdiscount', title: __('政策折扣(%)'),operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'main_free', title: __('应缴服务费'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'jingfei', title: __('净费'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'kehu_sumdiscount', title: __('客户折扣(%)'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'kehu_tongchou', title: __('客户服务费'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'jiafei_price', title: __('加费费用'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'shuidian', title: __('税点'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'fan', title: __('返费'), formatter: Table.api.formatter.status},
                        {field: 'yw_enter_peo', title: __('录入人'), operate: Config.sxstatus, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'yw_hetongpeo', title: __('审核人'), operate: Config.sxstatus, table: table, class: 'autocontent', formatter: function(value, row, index) {
                                if(value){
                                    return value;
                                }else{
                                    return '-';
                                }
                            }
                        },
                        {field: 'yw_belonger', title: __('Yw_belonger'), operate: Config.sxstatus, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'pay_fangshi', title: __('支付方式'), operate: Config.sxstatus, table: table, class: 'autocontent', formatter: function(value, row, index) {
                                if(value){
                                    return value;
                                }else{
                                    return '-';
                                }
                            }
                        },
                        {field: 'yw_join_time', title: __('Yw_join_time'), operate: Config.sxstatus, addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'yw_hetong_time', title: __('Yw_hetong_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        // {field: 'yw_shenhe_time', title: __('Yw_shenhe_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'sign', title: __('签字状态'), operate: false,visible:Config.status, table: table, class: 'autocontent', formatter: function(value, row, index) {
                                if(value == 1){
                                    var html = "已签字(<span style='color:red;cursor:pointer' onclick='qianzi("+row.id+","+row.gs_type+")'>重新签字</span>)";
                                    return html;
                                }else{
                                    var html = "<span onclick='copaylink("+row.id+","+row.gs_type+")'>点击复制签字链接</span>";
                                    return html;
                                }
                            }
                        },
                        {field: 'status', title: __('状态'), searchList: {
                                0: __('暂存'),
                                1: __('待审核'),
                                2: __('待市级审核'),
                                3: __('审核通过待缴费'),
                                4: __('待修改'),
                                5: __('已出单'),
                                6: __('已退统'),
                                7: __('已过期'),
                            },custom: {0:'success', 1:'default',2:'warning', 3:'info', 4:'danger', 5:'primary', 6:'huang', 7:'zi'},  formatter: Table.api.formatter.label
                        },
                        {field: 'car_factory_num', title: __('厂牌型号'),visible:false, operate: Config.sxstatus, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'details',
                                    text: '浏览',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: '',
                                    url: 'old/one/details',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //权限判断
                                        if (Config.rule.includes('254') || Config.rule.includes(254)) {
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('电子服务单'),
                                    title: __('电子服务单'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: '',
                                    url: 'old/one/dian',
                                    visible: function (row) {
                                        //权限判断
                                        if (Config.rule.includes('253') || Config.rule.includes(253)) {
                                            //返回true时按钮显示,返回false隐藏
                                            // if(row.status == 5){
                                                return true;
                                            // }
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        details: function () {
            Controller.api.bindevent();
        },
        bulu: function () {
            Controller.api.bindevent();
        },
        xubao: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    if ($("#treeview").length > 0) {
                        var r = $("#treeview").jstree("get_all_checked");
                        $("input[name='row[rules]']").val(r.join(','));
                    }
                    return true;
                });
            },
        }
    };
    //开始时间选择后
    $("#c-plan_strattime").on("dp.change",function(e){
        var time = $("#c-plan_strattime").val()
        gettimee(time)
        var end_time = $("#c-plan_endtime").val()
        var tianshu = diy_time(time,end_time)
        $("#datecount").html(tianshu)
    })
    //结束时间选择后
    $("#c-plan_endtime").on("dp.change", function() {
        var time = $("#c-plan_strattime").val()
        var end_time = $("#c-plan_endtime").val()
        var tianshu = diy_time(time,end_time)
        $("#datecount").html(tianshu)
    })
    function diy_time(time1,time2){
        time1 = Date.parse(new Date(time1));
        time2 = Date.parse(new Date(time2));
        return time3 =Math.abs(parseInt((time2 - time1)/1000/3600/24)+1)+'天';
    }
    function gettimee(date){
        $.ajax({
            type: 'post',
            url: "/api/order/s_time", //发送路径
            data: {
                time: date
            }, //发送的数据
            //                    dataType: 'json',
            async: false, //是否异步
            success: function(data) {
                $("#c-plan_endtime").val(data.data)
            }
        })
    }
    return Controller;
});
